<template>
  <basic-container>
    <div class="container category-wrap">
      <el-form :model="categoryForm" label-width="150px" label-position="right">
        <el-form-item label="打印机名称" required width="150px">
          <el-input v-model="categoryForm.printerName" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="打印机编号" width="150px" required>
          <el-input v-model="categoryForm.printerSn" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="选择商家" required>
          <el-select v-model="categoryForm.merchantId" placeholder="请选择">
            <el-option
              v-for="item in buyerList"
              :key="item.id"
              :label="item.merchantName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择店铺" required>
          <el-select v-model="categoryForm.shopId" placeholder="请选择">
            <el-option
              v-for="item in shopList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择打印机类型" required>
          <el-select v-model="categoryForm.printerType" placeholder="请选择">
            <el-option
              v-for="item in printer_type"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="打印机秘钥" width="150px" required>
            <el-input v-model="categoryForm.printerKey" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="选择打印机模板类型" required>
          <el-select v-model="categoryForm.templateType" placeholder="请选择">
            <el-option
              v-for="item in templateType"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="打印模板,小票样式" width="150px">
            <el-input type="textarea" placeholder="不填时使用默认" v-model="categoryForm.template" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="可打印分类" width="150px">
          <el-input v-model="categoryForm.category"  placeholder="可不填" class="form-input"></el-input>
        </el-form-item>
        <el-form-item label="打印次数" width="150px" required>
            <el-select v-model="categoryForm.times" placeholder="请选择">
              <el-option :key="1" label="1次" :value="1"></el-option>
              <el-option :key="2" label="2次" :value="2"></el-option>
              <el-option :key="3" label="3次" :value="3"></el-option>
              <el-option :key="4" label="4次" :value="4"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="是否添加至飞鹅云" width="150px" required>
            <el-select v-model="categoryForm.status" placeholder="请选择">
              <el-option
                v-for="item in yes_no"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="启用状态" width="150px" required>
            <el-select v-model="categoryForm.useable" placeholder="请选择">
              <el-option
              v-for="item in yes_no"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="备注信息" width="150px">
            <el-input v-model="categoryForm.remarks" type="textarea" class="form-input"></el-input>
        </el-form-item>
        <div>
            <el-button type="primary" size="medium" @click="modifyGoodsCategory">保存</el-button>
            <el-button type="default" size="medium" @click="close">取消</el-button>
        </div>
      </el-form>
    </div>
  </basic-container>
</template>

<script>
    import {
      addFeiePrinter
    } from '@/api/iot/feieprinter'
    import {
      mapGetters
    } from 'vuex'
    import Request from "../../manage/request.js";
    import {fetchDict} from '@/util/util';
    export default {
        name:'printer',
        data: function(){
            return {
              categoryForm:{
                category: "",
                merchantId: "",
                printerKey: "",
                printerName: "",
                printerSn: "",
                printerType: "",
                remarks: "",
                shopId: "",
                status: "1",
                template: "",
                templateType: "0",
                tenantId: '',
                times: "1",
                useable: "1",
              },
              printer_type:[],
              templateType:[],
              request: new Request(),
              shopList:[],//店铺列表
              buyerList:[],//商家列表
              yes_no:[],
              flag:1
            }
        },
        computed: {
          ...mapGetters(['tagWel', 'tagList', 'tag', 'website']),
          tagLen() {
            return this.tagList.length || 0
          }
        },
        created(){
            this.getShopList();
            this.getMerchantList();
            fetchDict('printer_type', this.printer_type);
            fetchDict('templateType', this.templateType);
            fetchDict('scm_yes_no', this.yes_no)
        },
        methods:{
          // 查询 店铺列表
          getShopList() {
            let params = {
              pageSize: 100,
              pageNo: 1,
            };
            this.request.getShopList(params).then(res => {
              if (res.code == 200) {
                this.shopList = res.ext.list;
              }else{
                this.$message.warning("未查询到采购商列表：" + res.msg)
              }
            })
          },
          // 查询商家列表
          getMerchantList() {
            let params = {
              pageSize: 100,
              pageNo: 1,
            };
            this.request.getMerchantList(params).then(res => {
              if (res.code == 200) {
                this.buyerList = res.ext.list;
                this.total = res.ext.count;
              }else{
                this.$message.warning("未查询到采购商列表：" + res.msg)
              }
            })
          },
          //添加
          modifyGoodsCategory() {
            if(this.categoryForm.printerName == ''){
              this.$message.warning("名称不可为空")
              return false;
            }
            if(this.categoryForm.printerSn == ''){
              this.$message.warning("编号不可为空")
              return false;
            }
            if(this.categoryForm.merchantId == ''){
              this.$message.warning("商家不可为空")
              return false;
            }
            if(this.categoryForm.shopId == ''){
              this.$message.warning("店铺不可为空")
              return false;
            }
            if(this.categoryForm.printerType == ''){
              this.$message.warning("打印机类型不可为空")
              return false;
            }
            if(this.categoryForm.printerKey == ''){
              this.$message.warning("打印机秘钥不可为空")
              return false;
            }
            if(this.categoryForm.templateType == ''){
              this.$message.warning("打印机模板类型不可为空")
              return false;
            }
            if(this.categoryForm.templateType == ''){
              this.$message.warning("打印机模板类型不可为空")
              return false;
            }
            if(this.flag != 1){
              return false;
            }
            this.flag = 0;
            let cate = this.categoryForm;
            addFeiePrinter(cate).then(res => {
              if (res.data.code == 0) {
                this.$message.success('添加成功');
                var _this = this;
                setTimeout(function () {
                  _this.close();
                },1500)
              } else {
                this.$message.error('添加失败')
                this.flag = 1;
              }
            })
          },
          close(){
            var str_before = location.href.split('#')[1];
            let { tag, key } = this.findTag(str_before);
            this.$store.commit('DEL_TAG', tag);
            this.$router.go(-1);
          },
          findTag(value) {
            let tag, key
            this.tagList.map((item, index) => {
              if (item.value === value) {
                tag = item
                key = index
              }
            })
            return { tag: tag, key: key }
          }
        }
    }
</script>

<style scoped>
  .el-form-item__content,.form-input{
    width: 300px;
  }
</style>
